package com.ysx.server.controller.admin;

import com.github.pagehelper.Page;
import com.ysx.server.pojo.dto.CommentPageQueryDTO;
import com.ysx.server.pojo.entity.Comment;
import com.ysx.server.pojo.vo.PageQueryVO;
import com.ysx.server.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/comment")
@Slf4j
public class CommentManageController {

    @Autowired
    private CommentService commentService;

    /**
     * 评论分页查询
     * @param commentPageQueryDTO
     * @return
     */
    @GetMapping("/comment")
    public PageQueryVO<Comment> commentPageQuery(CommentPageQueryDTO commentPageQueryDTO){
        log.info("评论分页查询：{}",commentPageQueryDTO);
        if(commentPageQueryDTO==null){
            return null;
        }
        Page<Comment> page = commentService.page(commentPageQueryDTO);
        long total = page.getTotal();
        List<Comment> result = page.getResult();
        return new PageQueryVO<Comment>(total,result);
    }

    /**
     * 修改评论信息
     * @param comment
     * @return
     */
    @PostMapping("/update")
    @CacheEvict(cacheNames = {"goodsCache_new","goodsCache_all","goodsCache"},allEntries=true)
    public boolean updateComment(@RequestBody Comment comment){
        if(comment==null){
            return false;
        }
        return commentService.update(comment);
    }

    /**
     * 删除评论信息
     * @param id
     * @return
     */
    @GetMapping("/delComment/{id}")
    @CacheEvict(cacheNames = {"goodsCache_new","goodsCache_all","goodsCache"},allEntries=true)
    public boolean delComment(@PathVariable Long id){
        if(id==null||id<0){
            return false;
        }
        return commentService.delComment(id);
    }
}
